package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.common.util.ResponseUtil;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.prometheus.client.Summary;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

//	使用云点播：
	@Autowired
	private VodService vodService;


	/**
	 * 保存声音
	 */
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
//		获取当前线程用户id
		Long userId = null == AuthContextHolder.getUserId()?1l:AuthContextHolder.getUserId();
//		0.保存声音文件、标题、分类、简介、声音所属专辑
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
//		1.返回数据
		return Result.ok();

	}

	/**
	 * 上传声音
	 */
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
//		1.调用服务层方法
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);

	}


	/**
	 * 分页查询声音列表
	 */
	@GuiguLogin
	@Operation(summary = "分页查询声音列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable("pageNo")Integer pageNo,
									@PathVariable("pageSize")Integer pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
//		1.获取用户id
		Long userId = null == AuthContextHolder.getUserId()?1l:AuthContextHolder.getUserId();
//		2.把用户id插进声音信息里
		trackInfoQuery.setUserId(userId);
//		3.新建分页插件
		Page<TrackInfoVo> trackInfoVoPage = new Page<>(pageNo,pageSize);
//		4.调用服务层方法
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackInfoVoPage,trackInfoQuery);
//		5.返回数据
		return Result.ok(trackListVoIPage);
	}


	/**
	 * 删除声音
	 */
	@Operation(summary = "根据声音id删除声音信息")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable("id") Long id){
//		调用服务层方法
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}


	/**
	 * 根据id获取声音信息
	 */
	@Operation(summary = "根据声音id获取声音信息")
	@GetMapping("/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("id") Long id){
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	/**
	 * 保存修改后的声音信息
	 */
	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateById(@PathVariable("id") Long id,
							 @RequestBody @Validate TrackInfoVo trackInfoVo){
		//从前端接收要修改声音的id，以及更新后的声音信息体，@Validate是开启参数校验
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}

	/**
	 * 根据专辑id获取声音列表
	 */
	@GuiguLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,
															  @PathVariable Long page,
															  @PathVariable Long limit){
//		1.获取用户id
		Long userId = AuthContextHolder.getUserId();
//		2.构建分页对象
		Page<AlbumTrackListVo> pageParam = new Page<>(page, limit);
//		3.调用服务层方法
		IPage<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(pageParam,albumId,userId);
		return Result.ok(pageModel);

	}


}

